package com.bii.gelapp;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MarkerDialog extends Dialog {
	public static Context Markercontext;
	public static Dialog d;
	RadioGroup rgMarkers;
	Button btSelect, btCancel, btDelete, btEdit;
	String analysistype;

	public MarkerDialog(Context context, String analysis) {
		super(context);
		this.Markercontext = context;
		this.analysistype = analysis;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.markerdialog);
		WindowManager.LayoutParams wmlp = this.getWindow().getAttributes();
		WindowManager wm = (WindowManager) this.Markercontext
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		wmlp.gravity = Gravity.CENTER_VERTICAL;
		wmlp.width = display.getWidth();
		d = MarkerDialog.this; 
		rgMarkers = (RadioGroup) findViewById(R.id.rgMarkers);
		// get radiobuttons titles through internal storage files
		ArrayList standardlist = new ArrayList();
		if (fileExistance("listofstandards")) {
			try {
				standardlist = (ArrayList) InternalStorage.readObject(
						Markercontext, "listofstandards");
				RadioButton rb;
				for (int i = 0; i < standardlist.size(); i++) {
					rb = new RadioButton(Markercontext);
					rb.setText(standardlist.get(i).toString());
					rb.setId(i);
					rgMarkers.addView(rb); // the RadioButtons are added to the
											// radioGroup instead of the layout

				}
				// allow users to create new one even though marker dialog is
				// populated
				RadioButton newMarkers = new RadioButton(Markercontext);
				newMarkers.setText("Create New Marker");
				newMarkers.setId(9999);
				rgMarkers.addView(newMarkers);
				// final RadioButton[] rb = new RadioButton[5];
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			RadioButton newMarkers = new RadioButton(Markercontext);
			newMarkers.setText("Create New Marker");
			newMarkers.setId(9999);
			rgMarkers.addView(newMarkers);
		}

		btSelect = (Button) findViewById(R.id.btSelect);
		btSelect.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (rgMarkers.getCheckedRadioButtonId() != -1) {
					RadioButton rb = (RadioButton) findViewById(rgMarkers
							.getCheckedRadioButtonId());
					String key = rb.getText().toString();
					try {
						if (key != "Create New Marker") {
							MarkerStandard ms1 = (MarkerStandard) InternalStorage
									.readObject(Markercontext, key);
							StartActivity.ms = ms1;
							//Intent gotonextactivity = new Intent(Markercontext,
							//		BrightnessActivity2.class);

							Intent gotonextactivity = new Intent(Markercontext,
							CropImageActivity.class);
							//9 september
							gotonextactivity.putExtra("analysisorigin", "Protein");
							
							dismiss();
							Markercontext.startActivity(gotonextactivity);
						} else {
							Intent gotonextactivity = new Intent(Markercontext,
									MarkerGaborActivity2.class);
							dismiss();
							Markercontext.startActivity(gotonextactivity);
						}

					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					// String selectedmarker = rb.getText().toString();
					// Intent gotonextactivity = new Intent(Markercontext,
					// BrightnessActivity2.class);
					// add bundle for marker arraylist here
					// dismiss();
					// Markercontext.startActivity(gotonextactivity);
				}
			}
		});
		btCancel = (Button) findViewById(R.id.btCancel);
		btCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		});

		// set variable in startActivity from dialog
		// ((MainActivity)GaussianFragment.this.getActivity()).setactiveBmp(activeBmp);
		btDelete = (Button) findViewById(R.id.btDelete);
		btDelete.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (rgMarkers.getCheckedRadioButtonId() != -1) {

					RadioButton rb = (RadioButton) findViewById(rgMarkers
							.getCheckedRadioButtonId());

					String key = rb.getText().toString();
					if (key != "Create New Marker") {
						DeleteDialog deleteDialog = new DeleteDialog(
								Markercontext, "MarkerDialog", key);
						deleteDialog.show();
					}

				}
			}
		});

		btEdit = (Button) findViewById(R.id.btEdit);
		btEdit.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent gotomarkerintent = new Intent(Markercontext,
						MarkerGaborActivity2.class);

				if (rgMarkers.getCheckedRadioButtonId() != -1) {
					RadioButton rb = (RadioButton) findViewById(rgMarkers
							.getCheckedRadioButtonId());
					String key = rb.getText().toString();
					try {
						if (key != "Create New Marker") {
							MarkerStandard ms1 = (MarkerStandard) InternalStorage
									.readObject(Markercontext, key);
							// StartActivity.ms = ms1;
							Intent gotonextactivity = new Intent(Markercontext,
									MarkerGaborActivity2.class);
							// put img source into object to be passed into
							// marker gabor activity
							gotonextactivity.putExtra("imgsrc", ms1.imgsrc);
							gotonextactivity.putExtra("mstitle", ms1.title);
							dismiss();
							Markercontext.startActivity(gotonextactivity);
						} else {
							// Intent gotonextactivity = new
							// Intent(Markercontext,
							// MarkerGaborActivity.class);
							dismiss();
							// Markercontext.startActivity(gotonextactivity);
						}

					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						Toast.makeText(Markercontext, "Error reading marker", Toast.LENGTH_SHORT).show();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						Toast.makeText(Markercontext, "Error reading marker", Toast.LENGTH_SHORT).show();
					}

				}
			}
		});

	}

	public boolean fileExistance(String key) {
		File file = Markercontext.getFileStreamPath(key);
		return file.exists();
	}

	public static void deleteFile(String key) {
		if (key != "Create New Marker") {
			
			ArrayList standardlist;
			try {
				standardlist = (ArrayList) InternalStorage.readObject(
						Markercontext, "listofstandards");
				for (int i = 0; i < standardlist.size(); i++) {
					if (standardlist.get(i).equals(key)) {
						standardlist.remove(i);
					}
				}
				MarkerStandard mcc = (MarkerStandard) InternalStorage.readObject(Markercontext, key);
				File file = new File(mcc.imgsrc);
				boolean deleted = file.delete();
				
				InternalStorage.deleteObject(Markercontext, key);
				InternalStorage.writeObject(Markercontext,
						"listofstandards", standardlist);
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} else {
			;
		}

		d.dismiss();
		Toast.makeText(Markercontext, "marker deleted", Toast.LENGTH_SHORT)
				.show();
	}
}